<template>
  <div v-if="contentLoaded" style="paddingBottom: 3rem">
      <Inviter></Inviter>
      <router-link to="/address" style="margin-top:.2rem">
			<AddressHeader :address="addressInfo"></AddressHeader>
		</router-link>
      <PayList></PayList>
	    <PayNote :bonus="bonus" :postage="postage" ph="请填写需充值的手机号码" v-model="note"></PayNote>
		<PayFooter :price="totalPrice" @pay="pay"
      :class="{disabled: user.level == 1}"></PayFooter>
  </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import Inviter from '@/components/Inviter'
import { Indicator, MessageBox, Toast } from 'mint-ui'
import { handleHistory } from '@/assets/js/utils'
import AddressHeader from '@/components/AddressHeader'
import PayList from './PayList'
import PayNote from './PayNote'
import PayFooter from './PayFooter'
import WxLogin from '@/api/wxlogin'
export default{
    name: 'BuyMembership',
    async created(){
    //   handleHistory(this.$router, '/usercenter')
      if(this.user.level == 1) {
        await MessageBox.alert('您已是创享会员，无需再次办理！')
        this.$router.go(-1)
      } else {
        this.settleMembershipGoods()
      }
    },
    data(){
      return{
        contentLoaded: false,
        note: ''
      }
    },
    computed:{
      ...mapGetters({
        user: 'user/userInfo',
        addressInfo: 'pay/addressInfo',
        goodsInfo: 'pay/goodsInfo',
        postage: 'pay/postage',
        bonus: 'pay/bonus',
        totalPrice: 'pay/totalPrice',
      }),
    },
    methods: {
      ...mapActions({
        getMembershipGoods: 'member/getMembershipGoods',
        addGoods: 'cart/addGoods',
        settleAccount: 'pay/toSettleCartGoods',
        submitOrder: 'pay/submitOrder',
		payOrder: 'pay/payOrder',
      }),
      async WxPayLogin() {
        // Indicator.open('支付环境监测中…')
        // await WxLogin.ifWxPayLoggedIn()
        // Indicator.close()
      },
      async settleMembershipGoods() {
          Indicator.open('加载中…')
          const {data, success} = await this.getMembershipGoods()
          if(success) {
             const goodsId = data.goods_list.goods_id
            await this.addGoods({
              goodsId: goodsId,
              goodsNum: 1
            })
            // 存入goodsId
            localStorage.setItem('selectedGoodsId', goodsId)
            await this.settleAccount()
            this.contentLoaded = true
          } else {
            await MessageBox.alert('暂无入会商品喔！')
            this.$router.replace('/usercenter')
          }
          Indicator.close()
      },
      async pay() {
        if(!this.note) Toast('请备注需充值手机号码')
        else if(!this.addressInfo) Toast('请填写地址')
        if(this.user.level == 1 || !this.note || !this.addressInfo) return
        Indicator.open()
        const orderFormLogId = await this.submitOrder({note: this.note})
        Indicator.close()
		this.payOrder({router: this.$router, orderFormLogId: orderFormLogId, type: 1})
      }
    },
    components: { Inviter, AddressHeader, PayList, PayNote, PayFooter }
}
</script>
